package com.example.nio.two;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @Description
 * @Author 26233
 * @Create 2021-10-28 16:25:55
 */

public class TaskThread implements Runnable {

    private TaskDispacther taskDispacther;

    public TaskThread(TaskDispacther taskDispacther) {
        this.taskDispacther = taskDispacther;
    }

    @Override
    public void run() {
        System.out.println("task thead start...");
        while (true){
            SocketChannel socketChannel = taskDispacther.getSocketChannelArrayBlockingQueue().poll();
            if (socketChannel == null){
                continue;
            }
            try {
                ByteBuffer recv = ByteBuffer.allocate(1024);
                int read = socketChannel.read(recv);
                System.out.println(Thread.currentThread() + " read = " + read);

                ByteBuffer send = ByteBuffer.wrap("ok\n".getBytes());
                int write = socketChannel.write(send);
                System.out.println(Thread.currentThread() + " write = " + write);

                // socketChannel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
